package edu.hubu.shop9.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hubu.shop9.common.JsonResult;
import edu.hubu.shop9.entity.Department;
import edu.hubu.shop9.entity.Purchasebill;
import edu.hubu.shop9.query.PurchasebillQuery;
import edu.hubu.shop9.service.IPurchasebillService;
import edu.hubu.shop9.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author waxijiang
 * @since 2021-07-29
 */
@Controller
@RequestMapping("/purchasebill")
public class PurchasebillController {
    @Autowired
    private IPurchasebillService purchasebillService;

    //	转发到我们展示数据的页面
    @RequestMapping("selectAll")
    public String selectAll() {
        return "purchasebill/purchasebillList";
    }

    @RequestMapping("/page")
    @ResponseBody
    public Map<String, Object> page(PurchasebillQuery query, int page, int rows) {
        Page<Purchasebill> page1 = new Page<>(page, rows);

        QueryWrapper<Purchasebill> wrapper = new QueryWrapper<>();
        Map<String, Object> fields = ObjectUtils.getNotNullFields(query);
        // 构造like条件
        for (Map.Entry<String, Object> entry : fields.entrySet()) {
            if (!"".equals(entry.getValue())) {
                if (entry.getValue() instanceof String) {
                    wrapper.like(entry.getKey(), entry.getValue());
                } else if (entry.getValue() instanceof Integer) {
                    wrapper.eq(entry.getKey(), entry.getValue());
                }
            }
        }

        IPage<Purchasebill> pageInfo = purchasebillService.selectByQuery(wrapper, page1);
        // 使用map传递到前台
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("rows", pageInfo.getRecords());
        return resultMap;
    }


    @RequestMapping("/deleteByKey")
    @ResponseBody
    public JsonResult deleteByKey(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            System.out.println("id：" + id);
            purchasebillService.removeById(id);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(HttpServletRequest request, Purchasebill purchasebill) {
        return saveOrUpdate(purchasebill);
    }


    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(HttpServletRequest request, Purchasebill purchasebill) {
        return saveOrUpdate(purchasebill);
    }


    private JsonResult saveOrUpdate(Purchasebill purchasebill) {
        JsonResult jsonResult = new JsonResult();
        try {
            purchasebillService.saveOrUpdate(purchasebill);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;

    }
}

